<?php
namespace Admin\Controller;
use Think\Controller;

/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/3/25
 * Time: 14:49
 */
class VirtualController extends CommonController
{
    private $model;
    public function __construct()
    {
        parent::__construct();
        $this->model = D('Virtual');
    }
    /**
     * 
    * 应用列表
    * @author wang
    * @param 
    */
    public function virtualList()
    {
        $where = array();
        $sort = I('request.sort', 'orderid');
        $order = I('request.order', 'asc');
        $server = I('server');
        if(!empty($server)) $where['title|description'] = array('like','%'.$server.'%');

        $result = $this->model->getList($where,$sort." ".$order);
        // $result = $this->model->getLastSql();        
        foreach ($result as $k => &$v) {
            $v['createtime']=date("Y-m-d H:i:s",$v['createtime']);
            $v['updatetime']=date("Y-m-d H:i:s",$v['updatetime']);
            $v['orderid']=(int)$v['orderid'];
        }
        $this->ajaxReturn($result);
    }

    /**
    * 增加Server
    * @author wang
    * @param 
    */
    public function addVirtual(){
    	$id = I('id');
        $data = I();
        $data['admin'] = session('admin');
        $data['updatetime'] = time();
    	if($id){
    		if($this->model->where("id='$id'")->save($data)){
            	$this->ajaxReturn(
                    array(
                        'success' => 1,
                        'msg' => '用户修改成功',
                        'id'=>$id
                        )
                    );
	        }else{
	            $this->ajaxReturn(
                    array(
                        'success' => 0, 
                        'msg' => '用户修改失败'
                        )
                    );
	        }
    	}else{
            // $data['id'] = (new  \Org\Twitter\IdWork(31,31))->nextId();
            $data['createtime']  = time();
    		if($this->model->add($data)){
            	$this->ajaxReturn(
                    array(
                        'success' => 1, 
                        'msg' => '用户新增成功'
                        )
                    );
	        }else{
	            $this->ajaxReturn(
                    array(
                        'success' => 0, 
                        'msg' => '用户增加失败'
                        )
                    );
	        }
    	}
    }

    /**
    * 删除Virtual
    * @author wang
    * @param 
    */
    public function delVirtual(){
        $id = I('id');
        if($this->model->where("id='$id'")->delete()){
            $result = array(
                'sta' => 1, 
                'msg' => '删除成功'
            );
        }else{
            $result = array(
                'sta' => 0, 
                'msg' => '删除失败'
            ); 
        }
        $this->ajaxReturn($result);
    }


    /**
      * 底分ID、服务器ID
      * @author wang
      * @param 
      */
      public function index(){
        $lowest_id = M('Unit')->select();
        $server_id = M('Server')->select();
        $this->assign("lowest_id",$lowest_id);
        $this->assign("server_id",$server_id);
        $this->display();
      }



}